<?php
// +----------------------------------------------------------------------
// | 品达网络 [ 品质保证，助企腾达 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.pdwl.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 萧涩啊 <wangmode@163.com>
// +----------------------------------------------------------------------
// | 员工管理

namespace app\models;
use think\Exception;

class Employee extends BaseModel
{
    protected $table = "sh_employee";
    /**
     * 员工列表
     * @param $page
     * @param $limit
     * @return array|\PDOStatement|string|\think\Collection|\think\model\Collection
     */
    public function getDataList($page,$limit){
        try {
            $where = [];
            return $this->where($where)->limit($page * $limit, $limit)->select();
        }catch (Exception $exception){
            return $this->setErrorInfo($exception->getMessage());
        }
    }


    /**
     * @param string $keyword
     * @return array|bool|\PDOStatement|string|\think\Collection|\think\model\Collection
     */
    public function getList($keyword='')
    {
        try {
            $where[] = ['e.status','=',1];
            if($keyword!==''){
                $where[] = ['e.name','like',"%$keyword%"];
            }
            return $this->alias('e')
                ->leftJoin('sh_department d','e.department_id=d.id')
                ->where($where)
                ->field('e.*,d.name as department')
                ->select();
        }catch (Exception $exception){
            return $this->setErrorInfo($exception->getMessage());
        }
    }

    /**
     * @param string $keyword
     * @return float|int|string
     */
    public function getCount($keyword='')
    {
        $where[] = ['status','=',1];
        if($keyword!==''){
            $where[] = ['name','like',"%$keyword%"];
        }
        return $this->where($where)->count();
    }


    public function add($data)
    {
        $data['add_time'] = time();
    }

    /**
     * @param $role_id
     * @return array|\PDOStatement|string|\think\Collection|\think\model\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getListByRole($role_id)
    {
        return self::alias('e')
            ->leftJoin('sh_role_user r','e.user_id=r.user_id')
            ->where('r.role_id',$role_id)
            ->field('e.id,e.name')
            ->select();
    }

    /**
     * 获取运营列表
     * @param $department_id
     * @return array|\PDOStatement|string|\think\Collection|\think\model\Collection
     */
    public function getOperationListByDepartmentId($department_id)
    {
        try {
            switch ($department_id) {
                case 1://短视频
                    $list = $this->getListByRole(7);
                    break;
                case 2://cct
                    $list = $this->getListByRole(6);
                    break;
                default :
                    $list = [];
            }
            return $list;
        }catch (Exception $exception){
            return self::setErrorInfo($exception->getMessage());
        }
    }

    /**
     * 获取业务列表
     * @param $department_id
     * @return array|\PDOStatement|string|\think\Collection|\think\model\Collection
     */
    public function getBusinessListByDepartmentId($department_id)
    {
        try {
            switch ($department_id) {
                case 1://短视频
                    $list = $this->getListByRole(11);
                    break;
                case 2://cct
                    $list = $this->getListByRole(5);
                    break;
                default :
                    $list =[];
            }
            return $list;
        }catch (Exception $exception){
            return self::setErrorInfo($exception->getMessage());
        }
    }




}
